(function(windom, $) {
	var $application = function(context) {
		for(var name in $application) {
			var app = $application[name];
			if($.isFunction(app.enable) ? app.enable() : app.enable === true) {
				app.init(context);
			}
		}
	};

	var $nojs = $application;
	//1  测试
	$nojs.test = {
		enable: function(context) {
			return true;
		},
		selector: ".test",
		init: function(context) {
			$(this.selector, context).each(function(i, t) {
				$(t).append("<h1>这是一个测试</h1>")
			});
		}
	};

	//2 UI-tabs
	$nojs.tabs = {
		enable: function() {
			return !!$.ui;
		},
		selector: ".ui-tabs",
		init: function(context) {
			$(this.selector, context).tabs();
		}
	};

	//3 UI-button
	$nojs.button = {
		enable: function() {
			return !!$.ui;
		},
		selector: ".ui-button",
		init: function(context) {
			$(this.selector, context).button();
		}
	};

	//Jquery-validate
	// 可在form 上指定data-debug=true，表示表单只验证不提交
	$nojs.validate = {
		enable: function() {
			return !!$.fn.validate;
		},
		selector: "form.validate",
		init: function(context) {
			var $forms = $(this.selector, context);
			var options = {
				ignore: ".novalidate", //对哪些元素不验证
				highlight: function(element) { //给未通过验证的元素加效果、闪烁等
					$(element).closest('.form-group').addClass('has-error');
				},
				unhighlight: function(element) {
					$(element).closest('.form-group').removeClass('has-error');
				},
				errorElement: "label", //指定使用什么标签标记错误
				errorClass: "help-inline",
				errorPlacement: function(error, element) { //把错误的体现显示在哪里
					$(element).closest(".form-group").append(error);
				}
			};
			$forms.each(function() {
				var $form = $(this);
				var validate = $form.validate($.extend(options, $form.data()));
			});
		}
	};
	$nojs.ueditor = { //初始化百度富文本编辑器  VIC  
		enable: function() {
			return !!window.UE;
		},
		selector: "script.u-editor",
		options: { //初始化的一些参数 尚未想好用哪些
		},
		init: function(context) {
			var module = this;
			$(this.selector, context).each(function(i, script) {
				var $script = $(this);
				var id = $script.attr("id");
				if(!id) {
					id = i + "ueditor";
					$script.attr("id", id);
				}
				var opt = $.extend({}, module.options, $script.data()); //可自定义传入一些参数
				console.log(opt);
				var ue = UE.getEditor(id, opt);
				$script.data("ueditor", ue);
			});
		}
	};

	//导航点击事件
	$nojs.navClick = {
		enable: true,
		selector: ".mine-nav li:not(:last)",
		init: function(context) {
			$(this.selector, context).click(function() {
				$(this).addClass("active").siblings("li").removeClass("active");
			});
		}
	};

	//日历控件
	$nojs.datetimepicker = {
		enable: function() {
			return !!$.fn.datetimepicker;
		},
		selector: "input[type=datetime]",
		options: {
			language: "zh-CN"
		},
		init: function(context) {
			var module = this;
			$(module.selector, context).each(function() {
				var $dt = $(this);
				var opt = $.extend(true, module.options, $dt.data());
				$dt.datetimepicker(opt);
			});

		}
	};

	//日历
	$nojs.timeCalendar = $.extend(true, {}, $nojs.datetimepicker, {
		selector: "div.calendar"
	});

	//提交表单动作 包括get  post  back
	$nojs.actions = {
		enable: true,
		init: function(context) {
			for(var name in this) {
				var module = this[name];
				if(module && module.init) {
					module.init(context);
				}
			}
		},
		get: { //get请求操作
			selector: ".action-get",
			option: {
				param: ".action-param" //请求参数
			},
			init: function(context) {
				var module = this;
				$(this.selector, context).click(function() {
					var $btn = $(this);
					//提示信息
					var confirm = $btn.data("confirm ");
					if(confirm) {
						if(!windom.confirm(confirm)) {
							return;
						}
					}
					//获取href
					var href = $btn.data("href");
					if(!/^\//.test(href)) { //如果不是以斜杠开头的href 则加上前缀
						var prefix = window.location.href;
						var index = prefix.indexOf("?");
						if(index != -1) {
							prefix = prefix.substring(0, index);
						}
						href = prefix + href;
					}
					//附加参数 包含在这 标签内部的 标签：<i class="action-param" data-key="key" data-value="val" ></i>
					var $params = $(module.option.param, $btn);
					if($params.length) {
						href += "?1=1";
						$params.each(function() {
							href += "&" + $(this).data("key") + "=" + encodeURIComponent($(this).data("value"));
						});
					}
					//此get请求是否是提交表单
					var formSelector = $btn.data("form");
					if(!formSelector) {
						window.location.href = href;
						return;
					}
					var $form;
					if($.type(formSelector) == "string") {
						$form = $(formSelector, context);
					} else {
						$form = $btn.parents("form");
					}
					if(!$form.length) {
						console.error("找不到绑定的表单");
						return;
					}
					$form.attr("action", href).attr("method", "get").submit();

				});
			}
		},
		post: { //post 请求操作
			selector: ".action-post",
			init: function(context) {
				$(this.selector, context).click(function() {
					var $btn = $(this);
					//提示信息
					var confirm = $btn.data("confirm ");
					if(confirm) {
						if(!window.confirm(confirm)) {
							return;
						}
					}
					//获取表单
					var $form = $btn.parents("form");
					if($btn.data("form")) {
						$form = $($btn.data("form"), context);
					}
					if(!$form.length) {
						$form = $('<form></form>')
					}
					$form.attr("method", "post");
					//获取href
					var href = $btn.data("href");
					if(href) {
						$form.attr("action", href);
					}
					$form.submit();
				});
			}
		},
		back: { //返回请求操作 只返回 不刷新
			selector: ".action-back",
			init: function(context) {
				$(this.selector, context).click(function() {
					window.history.back(); //返回
					//window.history.go(-1);//返回+刷新
				});
			}
		}
	};

	//checkbox 和全选/取消 和相关批量操作按钮 的关系建立
	$nojs.checkboxBinding = {
		selector: "[data-member]",
		$member: function($leader, context) { //获取成员
			var memberName = $leader.data("member"); //成员name
			return $("input:checkbox[name=" + memberName + "]:not(:disabled)", context);
		},
		$relate: function($leader, $member, context) { //相关操作按钮是否可操作
			var memberName = $leader.data("member"); //成员name
			var $relate = $("[data-checkbox-required=" + memberName + "]", context)
			if($relate.length !== 0) {
				$relate.prop("disabled", $member.filter(":checked").length === 0);
			}
		},
		init: function(context) {
			var module = this;
			$(this.selector, context).each(function() {
				var $leader = $(this);
				var $member = module.$member($leader, context);
				$leader.change(function() { //点击全选或者取消
					$member.prop("checked", $leader.prop("checked"));
					module.$relate($leader, $member, context);
				});
				$member.change(function() { //点击成员 若全选则全选checkbox选中
					$leader.prop("checked", $member.length == $member.filter(":checked").length);
					module.$relate($leader, $member, context);
				});
				module.$relate($leader, $member, context);
			});
		}
	};
	
	//chosen  下拉框
	$nojs.chosen = {
		enable: function(){
			return !!$.fn.chosen;
		},
		selector: "select.chosen",
		option :{
			no_results_text: "没有找到",//找不到结果时候显示的内容
    		allow_single_deselect: true,//是否允许取消选择
    		max_selected_options:12//当select为多选时，最多选择个数
		},
		init: function(context){
			var module = this;
			$(module.selector, context).each(function(){
				var $select = $(this);
				var opts = $.extend(module.option, $select.data());
				$select.chosen(opts);
			});
		}
	};

	windom.$nojs = windom.$application = $application;

})(window, jQuery);
$(document).ready(function() {
	window.$nojs(document);
});